package com.demo.steammanagement.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.steammanagement.common.QueryBuild;
import com.demo.steammanagement.common.ResultMsg;
import com.demo.steammanagement.config.response.BizException;
import com.demo.steammanagement.convert.AnnouncementConvert;
import com.demo.steammanagement.convert.GameAccountConvert;
import com.demo.steammanagement.model.entity.Announcements;
import com.demo.steammanagement.model.entity.GameAccounts;
import com.demo.steammanagement.model.vo.AnnouncementVO;
import com.demo.steammanagement.model.vo.GameAccountVO;
import com.demo.steammanagement.model.vo.Result;
import com.demo.steammanagement.service.IAnnouncementsService;
import com.demo.steammanagement.service.IGameAccountsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementsService iAnnouncementsService;

    @GetMapping("/page")
    public IPage<Announcements> page(AnnouncementVO vo) {

        if (ObjectUtils.isEmpty(vo)) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }

        Page<Announcements> page = new Page<>(vo.getPageNo(), vo.getPageSize());

        return iAnnouncementsService.page(page, QueryBuild.announcementQueryWrapper(vo));

    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(AnnouncementVO vo) throws IOException {
        return Result.success(iAnnouncementsService.saveOrUpdate(AnnouncementConvert.INSTANCE.convertVOToEntity(vo)));
    }

    @GetMapping("/get")
    public Result get(AnnouncementVO vo) {

        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(iAnnouncementsService.getById(vo.getId()));

    }

    @GetMapping("/delete")
    public Result delete(AnnouncementVO vo) {

        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(iAnnouncementsService.removeById(vo.getId()));

    }

}
